package kr.co.sharping;

import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

public class MyApplication extends Application {

	public static final String TAG = "VolleyPatterns";
	
	private static RequestQueue mRequestQueue;
	private static ImageLoader mImageLoader;
	
	private static MyApplication sInstance;
	
	private Gson gson;
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		sInstance = this;
	}
	
	public static synchronized MyApplication getInstance() {
		return sInstance;
	}
	
	public RequestQueue getRequestQueue() {
		
		if (mRequestQueue == null) {
			mRequestQueue = Volley.newRequestQueue(getApplicationContext());
		}
		
		return mRequestQueue;
	}
	
//	public ImageLoader getImageLoader() {
//		
//		if (mImageLoader == null) {
//			mImageLoader = new ImageLoader(getRequestQueue(), imageCache);
//		}
//		
//		return mImageLoader;
//	}
	
	public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);

        VolleyLog.d("Adding request to queue: %s", req.getUrl());

        getRequestQueue().add(req);
    }
	
	public <T> void addToRequestQueue(Request<T> req) {
        // set the default tag if tag is empty
        req.setTag(TAG);

        getRequestQueue().add(req);
    }
	
	public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

	public Gson getGson() {
		
		if (gson == null) {
			gson = new Gson();
		}
		
		return gson;
	}
}
